<!DOCTYPE html>
<html>
<head>
    <title>RickPan VIP支付</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 0;
            padding: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
        }
        .container {
            background: white;
            border-radius: 12px;
            padding: 40px;
            text-align: center;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            max-width: 400px;
        }
        .logo {
            font-size: 24px;
            font-weight: bold;
            color: #409eff;
            margin-bottom: 20px;
        }
        .loading {
            font-size: 16px;
            color: #606266;
            margin-bottom: 20px;
        }
        .spinner {
            width: 40px;
            height: 40px;
            border: 4px solid #f3f3f3;
            border-top: 4px solid #409eff;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin: 20px auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .error {
            color: #f56c6c;
            font-size: 14px;
            margin-top: 20px;
        }
        .back-link {
            color: #409eff;
            text-decoration: none;
            font-size: 14px;
            margin-top: 20px;
            display: inline-block;
        }
        .back-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="logo">🚀 RickPan VIP</div>
        <div class="loading">正在跳转到支付页面...</div>
        <div class="spinner"></div>
        <div id="error-message" class="error" style="display: none;"></div>
        <a href="javascript:history.back()" class="back-link" id="back-link" style="display: none;">返回上一页</a>
    </div>
    
    <div id="payment-form-container" style="display: none;"></div>
    
    <script>
        try {
            // 从URL参数获取HTML内容
            const urlParams = new URLSearchParams(window.location.search);
            const type = urlParams.get('type');
            let paymentHtml = '';
            
            if (type === 'base64') {
                // Base64解码方式
                const base64Data = urlParams.get('data');
                if (base64Data) {
                    paymentHtml = decodeURIComponent(escape(atob(base64Data)));
                }
            } else {
                // 原有的URL编码方式（作为备用）
                paymentHtml = urlParams.get('html') || '';
                if (paymentHtml) {
                    try {
                        paymentHtml = decodeURIComponent(paymentHtml);
                    } catch (e) {
                        // 如果解码失败，尝试不解码直接使用
                        console.warn('URL解码失败，尝试直接使用:', e);
                    }
                }
            }
            
            if (paymentHtml) {
                // 检查是否是完整的HTML页面（模拟支付）还是支付表单
                if (paymentHtml.includes('<!DOCTYPE html>') || paymentHtml.includes('<html>')) {
                    // 这是完整的HTML页面，使用document.write方式替换
                    document.open();
                    document.write(paymentHtml);
                    document.close();
                } else {
                    // 这是支付表单，使用原有的表单提交逻辑
                    document.getElementById('payment-form-container').innerHTML = paymentHtml;
                    
                    // 查找表单并自动提交
                    const form = document.querySelector('form');
                    if (form) {
                        // 3秒后自动提交，给用户一点时间看到加载画面
                        setTimeout(() => {
                            form.submit();
                        }, 1500);
                    } else {
                        throw new Error('未找到支付表单');
                    }
                }
            } else {
                throw new Error('支付信息为空');
            }
        } catch (error) {
            console.error('支付跳转失败:', error);
            
            // 显示错误信息
            const errorDiv = document.getElementById('error-message');
            const backLink = document.getElementById('back-link');
            const spinner = document.querySelector('.spinner');
            const loading = document.querySelector('.loading');
            
            loading.textContent = '支付跳转失败';
            spinner.style.display = 'none';
            errorDiv.textContent = '支付信息加载失败: ' + error.message + '，请返回重试';
            errorDiv.style.display = 'block';
            backLink.style.display = 'inline-block';
        }
    </script>
</body>
</html>